<template>
  <van-button
    :icon="value ? 'star' : 'star-o'"
    :class="{ cellected: value }"
    @click="onCollect"
    :loading="LoadingCellected"
  ></van-button>
</template>
<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'CollectArticle',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      LoadingCellected: false
    }
  },
  methods: {
    async onCollect () {
      this.LoadingCellected = true
      // 判断是否登录
      if (!this.$store.state.user) return this.$toast('请登录')

      try {
        if (this.value) {
          // 取消收藏
          await deleteCollect(this.articleId)
        } else {
          // 收藏
          await addCollect(this.articleId)
        }
        this.$emit('input', !this.value)
      } catch (error) {
        this.$toast('操作失败')
      }
      this.LoadingCellected = false
    }
  },
  created () {}
}
</script>
<style scoped lang="less">
.cellected {
  .van-icon {
    color: red;
  }
}
</style>
